﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GestionComercial.Model.Entities;
using GestionComercial.Model;
using GestionComercial.Utils;

namespace GestionComercial.Data
{
    public class RubroData
    {
        Context db = ManagerContext.Contexto;
        OperationResult result;
        public OperationResult Guardar(Rubro _Rubro)
        {
            result = new OperationResult();
            if (_Rubro != null)
            {
                try
                {
                    if (_Rubro.IdRubro.Equals(Guid.Empty) || _Rubro.IdRubro == null)
                    {
                        _Rubro.IdRubro = Guid.NewGuid();
                        db.Rubros.Add(_Rubro);
                    }
                    db.SaveChanges();
                    result.Id = _Rubro.IdRubro;
                    result.Success = true;
                    return result;
                }
                catch 
                {
                    result.Msg = "Error al guardar Rubro";
                    return result;
                }
            }
            result.Msg = "Erro, Rubro nulo";
            return result;
        }
        public OperationResult Eliminar(Rubro _Rubro)
        {
            result = new OperationResult();
            if (_Rubro == null)
            {
                result.Msg = "Rubro nulo";
                return result;
            }
            else 
            {
                db.Rubros.Remove(_Rubro);
                db.SaveChanges();
                result.Success = true;
                return result;
            }
        }
        public List<Rubro> CargarTodos()
        {
            return db.Rubros.ToList();
        }

        public Rubro CargarPorId(Guid IdRubro)
        {
            if ((IdRubro != null) && (!(IdRubro.Equals(Guid.NewGuid()))))
                return db.Rubros.FirstOrDefault(rub => rub.IdRubro == IdRubro);
            else
                return null;
        } 
    }
}
